package cn.jinjimi.app.presenter;

import android.content.Context;

import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.framework.helper.annotation.EventTraceParam;
import cn.jinjimi.framework.helper.annotation.EventTraceSelector;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventDataField;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.LocalEvent.CustomLasteFollowRefreshEvent;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.event.CustomTypeEvent;
import cn.jinjimi.app.custom.event.CustomTypeListEvent;
import cn.jinjimi.app.custom.event.CustomTypeTopEvent;
import cn.jinjimi.app.custom.event.CustomerListChangeEvent;
import cn.jinjimi.app.custom.iview.CustomTypeListView;
import cn.jinjimi.app.custom.manager.CustomsManager;
import cn.jinjimi.app.interfaces.IView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 客户分类列表
 * 包括 准客户
 * <p/>
 * 已签单客户
 * <p/>
 * Created by jinsen on 15/10/23.
 */
public class CustomerTypePresenter extends NetBasePresenter {

    private CustomTypeListView mView;
    private int mType;

    public CustomerTypePresenter(IView view, int type) {
        super(view);
        mView = (CustomTypeListView) view;
        mType = type;
    }

    public void requestData() {
        mView.loading();
        CustomsManager.getInstance().getCustomTypeList(mType);
    }

    //设置客户置顶状态
    public void setCustomerTopState(Custom custom, Context context) {
        if (custom == null)
            return;

        updateCustomerAndAnalyse(custom, context, custom.hasTop(), mType == Custom.TYPE_UNSOLD_CUSTOMER);
    }


    @EventTrace({EventID.APP_CUSTOMER_READYLIST_ONE_MARK, EventID.APP_CUSTOMER_PAIDLIST_ONE_MARK})
    private void updateCustomerAndAnalyse(Custom custom, Context context,
                                          @EventTraceParam(value = EventDataField.ParamType, selector = {EventDataField.TypeUntop, EventDataField.TypeTop}) boolean hasTop,
                                          @EventTraceSelector boolean unsoldCustomer) {
        if (hasTop) {
            custom.top = Custom.TAG_NORMAL;
            custom.topTime = 0;
        } else {
            custom.top = Custom.TAG_HAS_TOP;
            custom.topTime = System.currentTimeMillis();
        }
        mView.justLoading();
        CustomsManager.getInstance().updateTopState(custom);
    }

    //设置客户的类型
    @EventTrace(value = EventID.APP_CUSTOMER_READYLIST_ONE_MARK, paramsK = {EventDataField.ParamType}, paramsV = {EventDataField.TypePaid})
    public void setCustomerType(Custom custom, Context context) {
        mView.justLoading();
        CustomsManager.getInstance().updateCustomType(custom.customerId, Custom.TYPE_SOLDED_CUSTOMER);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomerListChangeEvent(CustomerListChangeEvent event) {
        CustomsManager.getInstance().getCustomTypeList(mType);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomTypeListEvent(CustomTypeListEvent event) {
        mView.setCustomers(event.customs);
        mView.loadSucc();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomTypeTopEvent(CustomTypeTopEvent event) {
        mView.resortCustoms();
        mView.loadSucc();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomTypeEvent(CustomTypeEvent event) {
        switch (event.state) {
            case DataEvent.SUCC:
                Custom custom = event.data;
                if (custom != null) {
                    mView.updateTypeSucc(custom);
                }
                mView.loadSucc();
                break;
            case DataEvent.FAIL:
                mView.showToast(R.string.error_bad_network);
                break;
        }
    }

    /* 监听跟进记录的变化 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomLasteFollowRefreshEvent(CustomLasteFollowRefreshEvent event) {
        CustomsManager.getInstance().getCustomTypeList(mType);
    }
}